package com.hyfrogx.modules.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.param.Ad;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.entity.HyAdEntity;
import com.hyfrogx.modules.manage.form.ad.AdForm;
import com.hyfrogx.modules.manage.form.ad.AdPutawayForm;
import com.hyfrogx.modules.manage.form.ad.AdVo;
import com.hyfrogx.modules.manage.service.HyAdService;
import com.hyfrogx.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/10/16 14:08
 * @Company 汉咏科技
 */
@RestController
@RequestMapping("/ad")
@Api(value = "HyAdController", tags = {"后台--广告管理"})
public class HyAdController extends AbstractController {

    @Autowired
    private HyAdService hyAdService;

    /**
     * 获取下拉类型集合
     */
    @PostMapping("/getType")
    @ApiOperation("获取下拉类型集合")
    public R GetType()
    {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("AdType",Ad.AdType.getCollection());
        map.put("JumpType",Ad.JumpType.getCollection());
        map.put("ModelType",Ad.ModelType.getCollection());
        return R.ok().put("data",map);
    }

    /**
     * 上传COS
     * @param file
     * @return
     */
    @ApiOperation("COS上传")
    @PostMapping("/upload")

    public R upload(@RequestParam("file") MultipartFile file) {
        // 文件路径
        String url = "";
        // 腾讯云
        url = UploadUtils.uploadWebFile(file, "/adverts");
        return R.ok().put("filename",url);
    }

    /**
     * 新增广告信息
     * @param adForm
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增广告信息")
    public R saveAd(@RequestBody @Validated AdForm adForm)
    {
        if (hyAdService.saveAd(adForm))
        {
            return R.ok();
        }
        else
        {
            return R.error("插入失败");
        }
    }

    /**
     * 根据参数查询广告信息
     */
    @PostMapping("/getByMap")
    @ApiOperation("分页查询广告信息")
    public R getAdsByMap(@RequestBody Map map)
    {
        QueryExt<HyAdEntity> queryExt =new QueryExt<HyAdEntity>(map);
        Map pmap = queryExt.getCondition();
        IPage<AdVo> iPage = hyAdService.getAdsByMap(queryExt,pmap);
        return R.ok().put("result",iPage);
    }

    /**
     * 编辑广告信息
     * @param adForm
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("编辑广告信息")
    public R updateAd(@RequestBody @Validated AdForm adForm)
    {
        if (hyAdService.updateAd(adForm))
        {
            return R.ok();
        }
        else {
            return R.error("编辑失败");
        }
    }

    @PostMapping("/putaway")
    @ApiOperation("广告上架")
    public R putaway(@RequestBody @Validated AdPutawayForm adPutawayForm)
    {
        if (hyAdService.putaway(adPutawayForm))
        {
            return R.ok();
        }
        else {
            return R.error("广告上架失败");
        }
    }
    /**
     * 根据id下架广告
     */
    @PostMapping("/soldOut/{id}")
    @ApiOperation("根据id下架广告")
    public R AdsoldOut(@PathVariable("id") Integer id)
    {
        if (hyAdService.AdsoldOutById(id))
        {
            return R.ok();
        }
        else {
            return R.error("广告下架失败");
        }
    }

    /**
     * 根据id撤销广告
     */
    @PostMapping("/repeal/{id}")
    @ApiOperation("根据id撤销广告")
    public R AdRepeal(@PathVariable("id") Integer id)
    {
        if (hyAdService.AdRepealById(id))
        {
            return R.ok();
        }
        else {
            return R.error("广告撤销失败");
        }
    }

    /**
     * 根据id删除广告
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("根据广告id删除广告")
    public R deleteById(@PathVariable("id") Integer id)
    {
        if (hyAdService.deleteById(id))
        {
            return R.ok();
        }
        else {
            return R.error("广告删除失败");
        }
    }
}
